package com.qiyu.common.utils;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * @author linhao
 * @date 2019/8/8
 * @Version V1.0
 */
public class HttpUtils {

    /**
     * 发送get请求
     *
     * @param url    请求url
     * @param params 参数信息
     * @return 响应信息
     */
    public static HttpResponse sendGet(String url, Map<String, String> params) {
        StringBuilder finalUrl = new StringBuilder(url);
        if (params.size() > 0) {
            finalUrl.append("?");
            int index = 0;
            int size = params.size();
            for (String key : params.keySet()) {
                index++;
                String param = params.get(key);
                finalUrl.append(key + "=" + param);
                if (index < size) {
                    finalUrl.append("&");
                }
            }
        }
        HttpGet httpGet = new HttpGet(finalUrl.toString());
        HttpClient httpClient = new DefaultHttpClient();
        try {
            return httpClient.execute(httpGet);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 发送post请求
     *
     * @param url    请求url
     * @param params json格式参数
     * @return 响应信息
     */
    public static HttpResponse sendPostInJson(String url, String params, Map<String, String> headerMap, String charSet) {
        HttpClient httpclient = new DefaultHttpClient();
        HttpPost httpPost = new HttpPost(url);
        for (String headerKey : headerMap.keySet()) {
            httpPost.setHeader(headerKey, headerMap.get(headerKey));
        }
        StringEntity entity = new StringEntity(params, charSet);
        httpPost.setEntity(entity);
        try {
            return httpclient.execute(httpPost);
        } catch (ClientProtocolException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 发送post请求 携带token
     *
     * @param url    请求url
     * @param params json格式参数
     * @return 响应信息
     */
    public static HttpResponse sendPostInJson(String url, String params) {
        HashMap<String, String> headerMap = new HashMap<>();
        headerMap.put("Accept", "application/json");
        headerMap.put("Content-Type", "application/json");
        return sendPostInJson(url, params, headerMap, "UTF-8");
    }



    /**
     * 请求微信接口
     *
     * @param url    请求url
     * @param postDataXML    参数
     * @return 微信返回的xml结果信息
     */
    public static String httpsRequestWX(String url, String postDataXML) throws IOException {
        HttpPost httpPost = new HttpPost(url);
        //解决XStream对出现双下划线的bug
        //得指明使用UTF-8编码，否则到API服务器XML的中文不能被成功识别
        StringEntity postEntity = new StringEntity(postDataXML,"text/xml", "UTF-8");
//        httpPost.addHeader("Content-Type", "text/xml");
        httpPost.setEntity(postEntity);
        System.out.println("请求的参数信息："+postDataXML);
        //设置请求器的配置
        RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(6500).setConnectTimeout(6500).build();
        httpPost.setConfig(requestConfig);
        HttpClient httpClient = HttpClients.createDefault();
        HttpResponse response = httpClient.execute(httpPost);
        HttpEntity entity = response.getEntity();
        String result = EntityUtils.toString(entity, "UTF-8");
        return result;
    }


}
